package com.dianjing.dianqun.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dianjing.dianqun.common.ResponseData;
import com.dianjing.dianqun.common.base.BaseRestController;
import com.dianjing.dianqun.model.DianQunUser;
import com.dianjing.dianqun.service.DianQunUserService;
import com.dianjing.dianqun.utils.AESUtil;
import com.dianjing.dianqun.utils.UserUtils;

/**
 * @date : 2017年10月9日
 * 
 * @author: baoyongtao
 * 
 * @see :
 * 
 */
@Controller
@RequestMapping("/user")
public class DianQunUserController extends BaseRestController {

	@Autowired
	private DianQunUserService dianQunUserService;
	
	@RequiresRoles("super")
	@RequestMapping("/change/pwd")
	public String index(HttpServletRequest request,Model model) {
		DianQunUser user = UserUtils.getUser();
		model.addAttribute("user", user);
		return "user/changePwd";
	} 
	
	
	
	@RequiresRoles("super")
	@RequestMapping("/password")
	@ResponseBody
	public Object changePassword(String oldPwd,String newPwd,String destPwd) {
		DianQunUser user = UserUtils.getUser();
		if(oldPwd!=null && newPwd!=null && newPwd.equals(oldPwd)) {
			String decodePwd = AESUtil.AESEncode(destPwd);
			if(decodePwd.equals(user.getPassword())) {
				user.setPassword(AESUtil.AESEncode(newPwd));
				dianQunUserService.update(user);
			}
		}
		return new ResponseData(0, "修改成功", user);
		
	} 
}
